import 'package:flutter/material.dart';
import 'package:m2_flutter/data/PrivacyPolicy.dart';
import 'package:m2_flutter/data/UserAgreement.dart';
import 'package:m2_flutter/service/CacheService.dart';

class AgreementPage extends StatefulWidget {
  final bool justLook;

  final bool? isAgreement;
  const AgreementPage({Key? key, required this.justLook, this.isAgreement})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _AgreementPage();
}

class _AgreementPage extends State<AgreementPage> {
  bool showAgreement = true;
  @override
  void initState() {
    super.initState();
    if (widget.isAgreement != null) {
      setState(() {
        showAgreement = widget.isAgreement!;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: SizedBox(
        child: Center(
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 10),
            child: Column(
              children: [
                Center(child: Text(showAgreement ? "服务条款" : "隐私协议")),
                const SizedBox(
                  height: 20,
                ),
                Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 20),
                            child: Text(showAgreement
                                ? UserAgreement.text
                                : PrivacyPolicy.text)),
                      ],
                    ),
                  ),
                ),
                if (widget.justLook)
                  InkWell(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 10),
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(10))),
                      child: const Center(
                        child: Text(
                          "确定",
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  )
                else
                  Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Padding(
                            padding: const EdgeInsets.all(10),
                            child: InkWell(
                              onTap: () {
                                setState(() {
                                  showAgreement = true;
                                });
                              },
                              child: Container(
                                padding: const EdgeInsets.all(10),
                                decoration: BoxDecoration(
                                    color: showAgreement
                                        ? Colors.blue
                                        : Colors.grey,
                                    borderRadius: const BorderRadius.all(
                                        Radius.circular(5))),
                                child: const Center(
                                  child: Text(
                                    "服务条款",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(10),
                            child: InkWell(
                              onTap: () {
                                setState(() {
                                  showAgreement = false;
                                });
                              },
                              child: Container(
                                padding: const EdgeInsets.all(10),
                                decoration: BoxDecoration(
                                    color: showAgreement
                                        ? Colors.grey
                                        : Colors.blue,
                                    borderRadius: const BorderRadius.all(
                                        Radius.circular(5))),
                                child: const Center(
                                  child: Text("隐私协议",
                                      style: TextStyle(color: Colors.white)),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      InkWell(
                        onTap: () {
                          Navigator.pop(context, false);
                        },
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                              vertical: 10, horizontal: 20),
                          child: const Center(
                            child: Text(
                              "不同意",
                            ),
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () async {
                          await CacheService.putString("isFirst", "false");
                          Navigator.pop(context, true);
                        },
                        child: Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 20, vertical: 10),
                            decoration: const BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(10)),
                                color: Colors.blue),
                            child: const Center(
                                child: Text(
                              "我已阅读并同意并继续",
                              style: TextStyle(color: Colors.white),
                            ))),
                      ),
                    ],
                  )
              ],
            ),
          ),
        ),
      ),
    ));
  }
}
